package com.example.teacher.Service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.common.constants.RdsConstants;
import com.example.common.domain.pojo.*;
import com.example.common.utils.CacheClient;
import com.example.teacher.Service.ClassesService;
import com.example.teacher.mapper.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

@Service
public class ClassesServiceImpl extends ServiceImpl<ClassesMapper, Classes> implements ClassesService {

    @Autowired
    private CacheClient cacheClient;
    @Autowired
    private StudentMapper studentMapper;
    @Autowired
    private TeacherMapper teacherMapper;
    @Autowired
    private ClassStudentMapper classStudentMapper;
    @Autowired
    private ClassTeacherMapper classTeacherMapper;


    @Override
    public Classes getClassesById(String id) {
        return cacheClient.queryWithPassThrough(
                RdsConstants.CLASSES_INFO_KEY,
                id,
                Classes.class,
                this::getById,
                RdsConstants.CACHE_TTL,
                TimeUnit.MINUTES,
                RdsConstants.CACHE_NULL_TTL,
                TimeUnit.MINUTES
        );
    }

    @Override
    public List<Classes> getClassesByIds(List<String> ids) {
        return super.baseMapper.selectBatchIds(ids);
    }

    @Override
    public List<Student> getStudentsByClassId(String classId) {
        LambdaQueryWrapper<ClassStudent> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ClassStudent::getClassId, classId);
        List<ClassStudent> classStudentList = classStudentMapper.selectList(queryWrapper);

        // 1. 提取学生id列表
        List<String> studentIdList = classStudentList.stream()
                .map(ClassStudent::getStudentId)
                .collect(Collectors.toList());

        // 2. 根据学生id列表查询学生信息
        if (studentIdList.isEmpty()) {
            return new ArrayList<>();
        }
        LambdaQueryWrapper<Student> studentQueryWrapper = new LambdaQueryWrapper<>();
        studentQueryWrapper.in(Student::getId, studentIdList);
        return studentMapper.selectList(studentQueryWrapper);
    }

    @Override
    public List<Teacher> getTeachersByClassId(String classId) {
        // 1. 根据 classId 查询 ClassTeacher 关联表记录
        LambdaQueryWrapper<ClassTeacher> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ClassTeacher::getClassId, classId);
        List<ClassTeacher> classTeacherList = classTeacherMapper.selectList(queryWrapper);

        // 2. 提取教师 ID 列表
        List<String> teacherIdList = classTeacherList.stream()
                .map(ClassTeacher::getTeacherId)
                .collect(Collectors.toList());

        // 3. 根据教师 ID 列表查询教师信息
        if (teacherIdList.isEmpty()) {
            return new ArrayList<>();
        }
        LambdaQueryWrapper<Teacher> teacherQueryWrapper = new LambdaQueryWrapper<>();
        teacherQueryWrapper.in(Teacher::getId, teacherIdList);
        return teacherMapper.selectList(teacherQueryWrapper);
    }
}
